package io.xccit.aischool.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.xccit.aischool.entity.Student;
import io.xccit.aischool.service.IStudentService;
import io.xccit.aischool.util.MD5;
import io.xccit.aischool.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CH_ywx
 * @date 2023-06-06
 * @description
 */
@Api(tags = "学生接口")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @ApiOperation("分页查询学生")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result<Page<Student>> getStudentByOpr(@ApiParam("页数") @PathVariable Integer pageNo,
                                                 @ApiParam("每页总条数") @PathVariable Integer pageSize,
                                                 @ApiParam("班级名称,精确查询字段") @RequestParam @Nullable String clazzName){
        Page<Student> studentPage = new Page<>(pageNo,pageSize);
        Page<Student> data = studentService.getStudentByOpr(studentPage,clazzName);
        return Result.ok(data);
    }

    @ApiOperation("添加/修改学生")
    @PostMapping("/addOrUpdateStudent")
    public Result<Boolean> saveOrUpdate(@ApiParam("学生请求体") @RequestBody Student student){
        String password = student.getPassword();
        student.setPassword(MD5.encrypt(password));
        return Result.ok(studentService.saveOrUpdate(student));
    }

    @ApiOperation("删除/批量删除学生")
    @DeleteMapping("/delStudentById")
    public Result<Boolean> deleteStudent(@RequestBody List<Integer> ids){
        return Result.ok(studentService.removeByIds(ids));
    }
}
